<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class Subrecycle extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
        // 引入的模型
        $this->SubjectModel = model('Subject.Subject');
        $this->ChapterModel = model('Subject.Chapter');
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->SubjectModel
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->SubjectModel
                ->onlyTrashed()
                ->with(['category'])
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo $this->SubjectOrderModel->getlastSql();
            // exit;
            $result = array("total" => $total, "rows" => $list);
            // var_dump($list);
            // exit;
            return json($result);
        }
        return $this->view->fetch();
    }

    // 真正删除
    public function del($ids = null)
    {

        // 课程ID
        $ids = empty($ids) ? [] : explode(',', $ids);

        // 章节ID
        $idlist = [];

        // 章节视频列表
        $urllist = [];

        // 课程图片列表
        $srclist = [];

        // 判断是否有没有当前数据  thumbs
        foreach ($ids as $item) {
            $res = $this->SubjectModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找当前课程');
            }

            // 获取所课程的图片地址
            array_push($srclist, $res['thumbs']);

            // 获取所课程下的章节ID
            $chapterid = $this->ChapterModel->where('subid', $item)->column('id');
            // 合并多个数组
            $idlist = array_merge($idlist, $chapterid);

            // 课程章节下的所有URL视频链接
            $chapterurl = $this->ChapterModel->where('subid', $item)->column('url');
            $urllist = array_merge($urllist, $chapterurl);
        }
        // 课程图片地址
        $srclist = array_filter($srclist);
        // 章节视频地址
        $urllist = array_filter($urllist);


        $subjectstatus = $this->SubjectModel->destroy($ids, true);

        if ($subjectstatus === FALSE) {
            $this->error($this->SubjectModel->getError());
        }

        $chapterstatus = $this->ChapterModel->destroy($idlist);
        if ($chapterstatus === FALSE) {
            $this->error($this->ChapterModel->getError());
        }

        if ($subjectstatus === FALSE || $chapterstatus === FALSE) {
            $this->error('课程删除失败');
        } else {

            // 批量删除视频文件
            foreach ($urllist as $item) {
                // $thumbs = substr($item, 1);
                // var_dump($item);
                // exit;
                $path = pathinfo($item, PATHINFO_DIRNAME);
                // var_dump($path);

                @is_file('.' . $item) && @unlink('.' . $item);
                @rmdir('.' . $path);//删除空目录
                // if (!$res) {
                //     $this->error('删除失败');
                //     exit;
                // } else {
                //     $this->success('删除成功');
                //     exit;
                // }

            }
            
            // 批量删除课程图片
            foreach ($srclist as $val) {
                // $src = substr($val, 1);
                // var_dump($val);
                @is_file('.' . $val) && @unlink('.' .$val);
                $path = pathinfo($val, PATHINFO_DIRNAME);
                @rmdir($path);//删除空目录
                // if (!$res) {
                //     $this->error('删除失败');
                //     exit;
                // } else {
                //     $this->success('删除成功');
                //     exit;
                // }
            }
            // exit;
            $this->success('课程删除成功');
        }
    }

    // 还原
    public function reduction($ids = null)
    {
        $id = empty($ids) ? [] : explode(',', $ids);
        $data = [];
        foreach ($id as $item) {
            $res = $this->SubjectModel->onlyTrashed()->find($item);
            if (!$res) {
                $this->error('未找到当前课程');
                exit;
            }
            $list = ['id' => $item, 'deletetime' => null];
            array_push($data, $list);
        }
        // 批量还原
        $result = $this->SubjectModel->isUpdate(true)->saveAll($data);
        if ($result) {
            $this->success('还原课程成功');
            exit;
        } else {
            $this->error($this->SubjectModel->getError());
            exit;
        }
    }
}